package utils;

import android.util.Log;

/**
 * Created by acer on 2018/10/11.
 */

public class JiaoYan {

    public static int AnalyData2(byte[] data, int nLen) {
        int nRet = 0;
        nRet = Analy55AA2(data, nLen);
        return nRet;
    }
    static int  g_nStep55AA=0;

    static   int  g_nLenFrame55AA=0;
    static  int  g_nPointData=0;
    static  int  g_nLenData=0;
    static   public byte[] g_bytesRecv = new byte[512];
    static  int  zonglen=0;
    public  static int Analy55AA2(byte[] data,int nLen)
    {
        zonglen += nLen;
        int nLenFrame= nLen;
        Log.e("7777", "xx"+nLenFrame);
        for(int i=0;i<nLenFrame;i++)
        {
            switch(g_nStep55AA)
            {
                case 0://55
                    if( (int)(data[i]&0xFF) == 0x55 )
                    {
                        g_nStep55AA = 1;
                        g_nLenFrame55AA = 0;
                        g_nPointData = 0;
                        g_nLenData = 0;
                        g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    }
                    break;
                case 1:
                    if( (int)(data[i]&0xFF) == 0xAA )
                    {
                        g_nStep55AA = 2;
                        g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    }
                    else
                        g_nStep55AA = 8;
                    break;
                case 2:
                    g_nStep55AA = 3;
                    g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    break;
                case 3:
                    if( (int)(data[i]&0xFF) == 0x00 )
                    {
                        g_nStep55AA = 4;
                        g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    }
                    else
                        g_nStep55AA = 8;
                    break;

                case 4:
//                    byte byteVerify = 0;
//                    for( int k = 2; k < g_nLenFrame55AA; k++ )
//                    {
//                        byteVerify ^= g_bytesRecv[k];
//                    }
//                    g_bytesRecv[g_nLenFrame55AA++] = data[i];
//                    if(	byteVerify == data[i] )
//                    {
//                        g_nStep55AA = STEP_55;
//                        zonglen2 = zonglen;
//                        zonglen = 0;
//                        return 0;
//                    }else
//                    {
//                        g_nStep55AA = STEP_55;
                        Log.e("test","计算校验"+HexUtil.BytesToString(g_bytesRecv,0,g_nLenFrame55AA));
                        return 2;//校验出错
//                    }
                    //break;
                default:
//                    g_nStep55AA = STEP_55;
                    return 3;
            }
        }
        return 1;
    }

}
